package games;

import utils.RandomNumber;
import graphics.Font5x5;
import hardware.Display2D;
import hardware.UI;

public class Game {

protected   int gameID=0;
protected   String gameName="default";	
protected   Highscore myHighscore = new Highscore();

protected int beginLevel=1;
protected int highestSelectableLevel=1;
protected int actualLevel=1;
protected int score;

Font5x5 myFont = new Font5x5();

public static final int CMD_NONE			= 0;
public static final int CMD_ABORT			= CMD_NONE+1;
public static final int CMD_SETUP			= CMD_NONE+1;
public static final int CMD_SELECT			= CMD_ABORT+1;
public static final int CMD_PREV			= CMD_SELECT+1;
public static final int CMD_NEXT			= CMD_PREV+1;



private static String levelString ="level";






public void displayLevelNumber() throws InterruptedException
{
	
	int counter;
	short color;

	color=0x4;
	
	checkKeysPressed();
	
	for (counter=0;counter<15;counter++)
		{
		Display2D.clear();
		//	color+=0x1007; //TODO Implement WIDE !!!!
		color+=0x07;
		
		Display2D.print8x8Char((char)(0x30+actualLevel), color, 0, 0);
		Display2D.doRepaint();
		
		
		if (checkKeysPressed()==CMD_SELECT)
			return;
		
		Thread.sleep(100);
		}
	
}


/**
 * Check keys of the right control stick and assign commands if a key is pressed 
 * @return command corresponding to pressed key
 * @throws InterruptedException 
 */


public int checkKeys()
{

	if (UI.keyRUp())
		return(CMD_SELECT);

	if (UI.keyRDown())
		return(CMD_ABORT);
	
	if (UI.keyRLeft())
		return(CMD_PREV);
		
	if (UI.keyRRight())
		return(CMD_NEXT);
	

	return(CMD_NONE);
}



public int checkKeysPressed()
{

	if (UI.keyPressed(UI.KEY_RIGHT_UP))
		return(CMD_SELECT);

	if (UI.keyPressed(UI.KEY_RIGHT_DOWN))
		return(CMD_ABORT);
	
	if (UI.keyPressed(UI.KEY_RIGHT_LEFT))
		return(CMD_PREV);
		
	if (UI.keyPressed(UI.KEY_RIGHT_RIGHT))
		return(CMD_NEXT);
	

	return(CMD_NONE);
}


/*
 *  Plays demonstration of a game - dummy for derived classes
 */



public int  playDemo() throws InterruptedException 
{
	return (CMD_NONE);	
}


/**
 * Display scrolled name of a Game
 * 
 * @return
 * @throws InterruptedException
 */


public int displayGameName() throws InterruptedException  
{
	int counter;
	int cmd;

	if (gameName==null)
		return (CMD_NONE);

	// TODO random Direction ??


	for (counter=0;counter<myFont.getTextSize(gameName);counter++)
		{
		Display2D.clear();
		
		myFont.displayText(gameName,6,counter,1,1,(short)0xff);
		Display2D.doRepaint();
		
		
		cmd=checkKeys();
		
		if (cmd!=CMD_NONE)
			return(cmd);

		Thread.sleep(100);

		
		}

	return (CMD_NONE);	
}




public int selectLevel() throws InterruptedException
{
	//levelString
	int counter;
	int cmd;
	int direction;
	int ledCounter;
	short color;
	direction=4;  //TODO randomize !!!
	
	checkKeysPressed();
	
	while(true)
		{
		direction=RandomNumber.getRange(8);
			
		color=(short) (RandomNumber.getRange(0xF) | RandomNumber.getRange(0xF) | RandomNumber.getRange(0xF));
		
		for (counter=0;counter<myFont.getTextSize(levelString);counter++)
			{
		
			cmd=checkKeysPressed();
		

			
			
			if (UI.keyPressed(UI.KEY_LEFT_LEFT) && (beginLevel>1))
				{
				beginLevel--;
				}
			else if (UI.keyPressed(UI.KEY_LEFT_RIGHT) && (beginLevel<highestSelectableLevel))
				{
				beginLevel++;
				}
			
			else if ((cmd!=CMD_NONE))
				return(cmd);


			Display2D.clear();
			
			myFont.displayText(levelString,direction,counter,1,1,color);
	
			for (ledCounter=0;ledCounter<beginLevel;ledCounter++)
				{
				if (ledCounter>((highestSelectableLevel*3)/4))
					{
					Display2D.setTopLED(ledCounter, (short) 0xf);	
					}
				else if (ledCounter>(highestSelectableLevel/2))
						{
						Display2D.setTopLED(ledCounter, (short)0x8f);
						}
					
					else 
						{
						Display2D.setTopLED(ledCounter, (short)0xf0);
						}
				
				}

			Display2D.doRepaint();
			
			Thread.sleep(100);
			}
		}

	
}


public int setupAcc()
{
	int actualAxis;
	int cmd;
	
	actualAxis=0;
	
	while(true)
	{
		Display2D.clear();
		
		switch (actualAxis)
		{
		case 0:
			Display2D.drawLine(0, 0, 0, 5, (short)0x8);
			Display2D.drawLine(0, 0, 4, 4, (short)0x8);
			Display2D.drawLine(0, 0, 5, 0, (short)0xf0);
			break;

		case 1:
			Display2D.drawLine(0, 0, 5, 0, (short)0x8);
			Display2D.drawLine(0, 0, 4, 4, (short)0x8);
			Display2D.drawLine(0, 0, 0, 5, (short)0xf0);
			break;
			
		case 2:
			Display2D.drawLine(0, 0, 5, 0, (short)0x8);
			Display2D.drawLine(0, 0, 0, 5, (short)0x8);
			Display2D.drawLine(0, 0, 4, 4, (short)0xf0);
			break;
	
		}
		
		cmd=checkKeysPressed();

		if (UI.keyPressed(UI.KEY_LEFT_UP) & (actualAxis<2))
				actualAxis++;
	
		if (UI.keyPressed(UI.KEY_LEFT_DOWN) & (actualAxis>0))
			actualAxis--;
		

		
		Display2D.doRepaint();

		if (cmd!=CMD_NONE)
			return(cmd);
		
	}
	
	
}


public int displayScore(int score) throws InterruptedException
{
	char [] scoreText= new char [5];
	int counter;
	
	int tmpValue;
	int size=0;

	
	tmpValue=score;
	
	do {
		tmpValue/=10;
		size++;
	} while (tmpValue>0);

//	System.out.println(" score : "+score);
	

	
	for (counter=0;counter<size;counter++)
		{
		scoreText[size-counter-1]=(char) (0x30+(score % 10));
		score/=10;
		
	//	System.out.println(scoreText[counter]);
		
		}
	
	int x;
	int a;
	
	int start;
	int end;
	int tmp;
	int faktor;
	
	faktor=1;
	
	start=0;
	end=(size-1)*6;
	
	while (true)   // TODO MURKS !!!!!!
	{
		x=0;

		tmp=start;
		start=end;
		end=tmp;
		faktor=-faktor;
		
		for (a=start;a<end;a++)
			{
			Display2D.clear();
			
			
			
			for (counter=0;counter<size;counter+=faktor)
				{
				Display2D.print5x5Char(scoreText[counter], (short) 0x770, x,0);
				x+=6;
				}

			Display2D.doRepaint();
			Thread.sleep(200);
			x=-a;
			
int cmd;
			
			cmd=checkKeysPressed();


			

			if (cmd!=CMD_NONE)
				return(cmd);
			
			}
		
		
		
	}
	
	
	
//	return (CMD_NONE);		
}



// insertHighscore
public int displayHighscores()
{
	return (CMD_NONE);		
}

}
